import React from 'react';
import { Navigate, useLocation } from 'react-router-dom';
import { useAuth } from 'lfun-bg-comp';
import BasicLayout from '@/layout/basic-layout';

// eslint-disable-next-line react/require-default-props
const AuthRoute: React.FC<{ isFullPage?: boolean; isShowMenu?: boolean; component: React.ComponentType }> = props => {
  const { component: Component, isFullPage = true, isShowMenu = true } = props;
  const auth = useAuth();
  const location = useLocation();

  if (auth.isLogin()) {
    return isFullPage ? (
      <BasicLayout isShowMenu>
        <Component />
      </BasicLayout>
    ) : (
      <Component />
    );
  }

  return <Navigate to="/login" state={{ from: location }} replace />;
};

export default AuthRoute;
